﻿/* InputLanguage 扩展类
 */

/*待测试*/
namespace LSLib.Windows.Forms
{
    using System;
    using System.Globalization;
    using System.Windows.Forms;

    public static class InputLanguageEx
    {
        /// <summary>获取：系统当前输入法
        /// </summary>
        public static InputLanguage GetCurrentInputLanguage()
        {
            return InputLanguage.CurrentInputLanguage;
        }

        /// <summary>获取：系统默认输入法
        /// </summary>
        public static InputLanguage GetDefaultInputLanguage()
        {
            return InputLanguage.DefaultInputLanguage;
        }

        /// <summary>获取：系统已安装的所有输入法
        /// </summary>
        public static InputLanguageCollection GetInstalledInputLanguages()
        {
            return InputLanguage.InstalledInputLanguages;
        }

        /// <summary>设置：默认输入法
        /// </summary>
        public static void SetInputLanguage_Default()
        {
            SetInputLanguage(InputLanguage.DefaultInputLanguage);
        }

        /// <summary>设置：英文输入法
        /// </summary>
        public static void SetInputLanguage_English()
        {
            CultureInfo cultureEnglish = CultureInfo.GetCultureInfo("en-US");
            SetInputLanguage(cultureEnglish);
        }

        /// <summary>设置指定输入法：区域性信息
        /// </summary>
        /// <param name="cultureInfo">区域性信息</param>
        public static void SetInputLanguage(CultureInfo cultureInfo)
        {
            InputLanguage inputLanguage = InputLanguage.FromCulture(cultureInfo);
            SetInputLanguage(inputLanguage);
        }
        /// <summary>设置指定输入法：输入法索引
        /// </summary>
        /// <param name="inputLanguageIndex">输入法索引</param>
        public static void SetInputLanguage(int inputLanguageIndex)
        {
            int index = inputLanguageIndex;//索引
            int count = InputLanguage.InstalledInputLanguages.Count;//输入法数量

            if (index >= count) throw new ArgumentOutOfRangeException("inputLanguageIndex");

            SetInputLanguage(InputLanguage.InstalledInputLanguages[index]);
        }
        /// <summary>设置指定输入法：输入法名称
        /// </summary>
        /// <param name="p_InputLangName">输入法名称</param>
        public static void SetInputLanguage(string inputLanguageName)
        {
            foreach (InputLanguage next in InputLanguage.InstalledInputLanguages)
            {
                if (next.LayoutName.Equals(inputLanguageName))
                {
                    SetInputLanguage(next);
                    return;
                }
            }

            throw new Exception(string.Format("【{0}】输入法不存在！", inputLanguageName));
        }
        /// <summary>设置指定输入法：输入法对象
        /// </summary>
        /// <param name="p_InputLang">输入法对象</param>
        public static void SetInputLanguage(InputLanguage inputLanguageObject)
        {
            if (inputLanguageObject == null) throw new ArgumentNullException("inputLanguageObject");
            if (InputLanguage.InstalledInputLanguages.Contains(inputLanguageObject) == false) throw new Exception("输入法对象不存在！");

            InputLanguage.CurrentInputLanguage = inputLanguageObject;
        }
    }
}
